package com.lw.extraceim.ui.fragments;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.fragment.app.Fragment;

import com.google.gson.reflect.TypeToken;
import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;
import com.lw.extraceim.R;
import com.lw.extraceim.app.ExtraceApplication;
import com.lw.extraceim.loader.PackageLoader;
import com.lw.extraceim.model.TransPackage;
import com.lw.extraceim.net.HttpResponseParam;
import com.lw.extraceim.net.ResponseCode;
import com.lw.extraceim.net.callback.HttpResponseParamCallback;
import com.lw.extraceim.ui.activity.es.WriteExpressSheetActivity;
import com.lw.extraceim.ui.activity.tp.PackageEditActivity;
import com.lw.extraceim.ui.activity.tp.PackageInfoActivity;
import com.lw.extraceim.ui.adapter.TransPackageAdapter;
import com.lw.extraceim.ui.fragments.base.BaseFragment;
import com.lw.extraceim.util.JsonUtils;
import com.lw.extraceim.util.ZxingUtil;
import com.lw.extraceim.zxing.ExtraceCaptureActivity;

import org.json.JSONException;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;

/**
 * @Author : qutangjun
 * @ChangeBy : liwei
 * @Description :
 * @Date : 2019/4/20 9:24
 */
public class PackagePageFragment extends BaseFragment {
    private static final String TAG = "PackagePageFragment";
    private LinearLayout mLlCreateNewPackageLayout;//创建新包裹的布局
    private Button mButtonScanNewPackage;//扫描新包裹的按钮
    private ListView mLvNewPackageList;//新建包裹列表
    private TextView mTvNoData;//没有数据时显示的文字

    private List<TransPackage> transPackageList = new ArrayList<>();//状态为新建的包裹列表

    private TransPackageAdapter adapter;
    private PackageLoader loader;


    public PackagePageFragment(Context mContext, ExtraceApplication application) {
        super(mContext, application);
    }

    @Override
    public View initView() {
        View view = View.inflate(mContext, R.layout.layout_package_page_fragment, null);
        //创建新包裹布局初始化
        mLlCreateNewPackageLayout = view.findViewById(R.id.ll_create_package_layout);
        mButtonScanNewPackage = view.findViewById(R.id.button_scan_new_pacakge);
        mLvNewPackageList = view.findViewById(R.id.lv_new_package_list);
        mTvNoData = view.findViewById(R.id.tv_no_data);
        return view;
    }


    /*
     * 初始化点击事件
     */
    @Override
    public void initEvent() {
        mButtonScanNewPackage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ZxingUtil.startQRScan(getActivity(), ExtraceCaptureActivity.class, "扫描条形码", ExtraceCaptureActivity.SCAN_NEW_PACKAGE);
            }
        });
        mLvNewPackageList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                TransPackage transPackage = transPackageList.get(position);
                if (transPackage.getStatus() == TransPackage.PACKAGE_NEW) {
                    PackageEditActivity.actionStart(getActivity(), transPackage);
                } else {
                    PackageInfoActivity.actionStart(getActivity(), transPackage);
                }
            }
        });

    }

    @Override
    public void initData() {
        loader = new PackageLoader(getContext());
        loadPackageList();
    }


    /**
     * 加载包裹列表
     */
    public void loadPackageList() {
        loader.getPackageListByUserId(application.getUserInfo().getuId(), 0, TransPackage.PACKAGE_PACK, new HttpResponseParamCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                alert("网络错误" + e.getMessage());
            }

            @Override
            public void onResponse(HttpResponseParam response, int id) {
                Log.d(TAG, "onResponse: " + response.reseponseJson);
                if (response.code.getCode() == ResponseCode.SUCESS) {
                    try {
                        transPackageList = JsonUtils.fromJson(response.reseponseJson.getJSONArray("packageList").toString(), new TypeToken<List<TransPackage>>() {
                        });
                        for (TransPackage transPackage : transPackageList) {
                            transPackage.onLoad();
                        }
                        refreshPackageList();
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                } else {
                    alert(response.code.getMessage());
                }
            }
        });
    }

    @Override
    public void refreshUI() {
        refreshPackageList();
    }


    /**
     * 刷新包裹列表
     */
    public void refreshPackageList() {
        mLvNewPackageList.setVisibility(FrameLayout.VISIBLE);
        mLvNewPackageList.setAdapter(new TransPackageAdapter(transPackageList, getActivity()));
        mTvNoData.setVisibility(FrameLayout.GONE);

    }

    @Override
    public void onResume() {
        super.onResume();
        loadPackageList();
    }
}
